<template>
    <page-container title="银行卡" :showBack="true">
        <template v-slot:nav-left>
            <view class="nav-left" @click="goBack">
                <uni-icons type="left" size="20" color="#d4af37"></uni-icons>
            </view>
        </template>
        <view class="bank-card-page">
            <!-- 顶部标题栏 -->
            <view class="header">
                <view class="title">我的卡 <text class="card-count">({{ cardList.length }}张)</text></view>
                <view class="add-btn" @click="addCard">
                    <uni-icons class="add-icon" type="plusempty" size="12" color="#0B1F20"></uni-icons>
                </view>
            </view>

            <!-- 银行卡列表 -->
            <view class="card-list" v-if="cardList.length > 0">
                <view class="bank-card" v-for="card in cardList" :key="card.id" @click="selectCard(card)">
                    <view class="card-content">
                        <view class="card-left">
                            <view class="bank-logo">
                                <image :src="card.logo" class="logo-img"></image>
                            </view>
                            <view class="bank-info">
                                <text class="bank-name">{{ card.bankName }}</text>
                                <text class="card-type">{{ card.cardType || '储蓄卡' }}</text>
                            </view>
                        </view>
                        <view class="card-number">
                            <!-- <text class="masked-number">•••• •••• ••••</text> -->
                            <text class="last-four">{{ card.bankCardNo }}</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- 空状态 -->
            <view class="empty-state" v-if="cardList.length === 0">
                <text class="empty-text">暂无银行卡</text>
                <text class="empty-desc">点击右上角添加银行卡</text>
            </view>
        </view>
    </page-container>
</template>

<script>
export default {
    name: 'BankCard',
    data() {
        return {
            cardList: []
        }
    },
    methods: {
        goBack() {
            setTimeout(() => {
                uni.navigateBack()
            }, 100)
        },
        addCard() {
            uni.navigateTo({
                url: '/pages/mine/bankCard/addCard'
            })
        },
        selectCard(card) { },
        // 获取银行卡列表
        getCardList() {
            this.$http.get('/applet/customer/bankCardList').then(res => {
                this.cardList = res.data || []
            }).catch(err => {
                console.error('获取银行卡列表失败:', err);
            })
        },
        refreshBankCardList() {
            this.getCardList()
        }
    },
    onLoad() {
        this.getCardList()
    },
    onShow() {
        // 监听刷新银行卡列表事件
        uni.$on('refreshBankCardList', this.refreshBankCardList)
        // 每次显示页面时也刷新一次列表
        this.getCardList()
    },
    onHide() {
        // 移除事件监听，避免内存泄漏
        uni.$off('refreshBankCardList', this.refreshBankCardList)
    },
    onUnload() {
        // 页面卸载时移除事件监听
        uni.$off('refreshBankCardList', this.refreshBankCardList)
    }
}
</script>

<style lang="scss" scoped>
.bank-card-page {
    min-height: 100vh;
    background-color: #f5f5f5;
    padding: 20rpx 26rpx;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0rpx 0 20rpx 26rpx;

    .title {
        font-size: 36rpx;
        font-weight: 500;
        color: $uni-text-color-black;

        &::before {
            content: '';
            display: inline-block;
            width: 8rpx;
            height: 32rpx;
            background: $uni-text-color-black;
            margin-right: 10rpx;
        }

        .card-count {
            color: #999999;
            font-size: 28rpx;
            font-weight: 400;
            margin-left: 10rpx;
        }
    }

    .add-btn {
        width: 60rpx;
        height: 60rpx;
        background: $uni-text-color-orange;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;

        .add-icon {
            color: $uni-text-color-black;
            font-size: 32rpx;
            font-weight: bold;
        }
    }
}

.card-list {
    .bank-card {
        margin-bottom: 30rpx;

        .card-content {
            background: linear-gradient(135deg, #d5b273 0%, #edce87 100%);
            border-radius: 16rpx;
            padding: 40rpx 30rpx;
            box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
            position: relative;
            overflow: hidden;

            .card-left {
                display: flex;
                align-items: center;
                margin-bottom: 40rpx;

                .bank-logo {
                    width: 80rpx;
                    height: 80rpx;
                    background: #eadbb4;
                    border-radius: 50%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-right: 20rpx;

                    .logo-img {
                        width: 40rpx;
                        height: 40rpx;
                    }
                }

                .bank-info {
                    display: flex;
                    flex-direction: column;

                    .bank-name {
                        color: #fff;
                        font-size: 28rpx;
                        margin-bottom: 8rpx;
                    }

                    .card-type {
                        color: #fff;
                        font-size: 24rpx;
                        opacity: 0.9;
                    }
                }
            }

            .card-number {
                display: flex;
                align-items: center;

                .masked-number {
                    color: #fff;
                    font-size: 32rpx;
                    letter-spacing: 6rpx;
                    margin-right: 30rpx;
                    opacity: 0.9;
                }

                .last-four {
                    color: #fff;
                    font-size: 32rpx;
                    font-weight: 600;
                    letter-spacing: 2rpx;
                }
            }
        }
    }
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top: 200rpx;

    .empty-text {
        font-size: 32rpx;
        color: #999;
        margin-bottom: 20rpx;
    }

    .empty-desc {
        font-size: 28rpx;
        color: #ccc;
    }
}
</style>
